package com.qf.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MQlistener {

    @RabbitListener(queues = "user-queue")
    public void userQueue(String msg, Channel channel, Message message) throws Exception {

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        System.out.println("deliveryTag:" + deliveryTag);
        // false：值应答一条消息
        // true:代表应答所有小于deliveryTag
        if ("error".equals(msg)) {
            // 模拟消费出现异常了
            System.out.println("消息【" + msg + "】出现异常。。。");
            // 第三个参数：拒绝接受
            channel.basicNack(deliveryTag,false,false);
            throw new RuntimeException();
        } else {
            System.out.println("业务队列消费的消息:" + msg);
            channel.basicAck(deliveryTag, false);
        }
    }

    @RabbitListener(queues = "dlq-queue")
    public void dlqQueue(String msg, Channel channel, Message message) throws Exception {
        System.out.println("死信队列消费数据:"+msg);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
